package com.qf.edu.controller;

import com.qf.edu.entity.Users;
import com.qf.edu.service.ImgPathService;
import com.qf.edu.service.LoginService;
import com.qf.edu.service.impl.ImgPathServiceImpl;
import com.qf.edu.service.impl.LoginServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author:huayuzhuo
 * @email:2798866329@qq.com
 * @desc:登录页面控制类
 * @datatime: 2022/5/24 10:46
 */
@WebServlet("/loginCom")
public class LoginCom extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String typeId = req.getParameter("typeId");
        Users users = new Users();
        users.setName(name);
        users.setPassword(password);
        users.setJobName(typeId);

        //将登录页面的数据传给session
        HttpSession session = req.getSession();
        session.setAttribute("user",new Users());

        LoginService loginService = new LoginServiceImpl();
        Users users1 = loginService.loginService(users);
        if (users1 != null){
            System.out.println(name + typeId + "" + "登陆验证已通过");
            System.out.println("密码：" + password);
            //传递session
            session.setAttribute("name",name);
            session.setAttribute("password",password);
            //获取头像相对路径
            ImgPathService imgPathService = new ImgPathServiceImpl();
            String path = imgPathService.getPath(name);
            session.setAttribute("img",path);

            resp.sendRedirect("/nurse/nurse.jsp");
        } else {
            System.out.println("登录失败");
            resp.sendRedirect("/login.html");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
